<?php
/**
 * @Project  Social Media Links
 * @author   Bryan Keller
 * @package  
 * @copyright Copyright (C) 2011 Techheads IT Consulting. All rights reserved.
 * @license  http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL version 2
*/

// Check to ensure this file is included in Joomla!
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

class plgContentSocialMediaLinks extends JPlugin {
	var $p_type = '';
	var $p_name = '';

  function plgContentSocialMediaLinks( &$subject,$params ) {
	$this->p_type = $params['type'];
	$this->p_name = $params['name'];
    	parent::__construct( $subject,$params );
  }
  function onContentPrepare($context, &$article, &$params, $page=0){
    
    $view     = JRequest::getCmd('view');
  }

	function onAfterRender(){
		$mainframe = JFactory::getApplication();
		
		if($mainframe->isAdmin()){
			return true;
		}
		
		// Get plugin info
		$plugin =& JPluginHelper::getPlugin($this->p_type, $this->p_name);
		$pluginParams = new JParameter( $plugin->params );
		
		ob_start();

		if ($pluginParams->get('sidebar') == "no"){
			return true;
		}

    	$document 	= & JFactory::getDocument();
    	$title = $document->getTitle();
	$uri = & JURI::getInstance();
     	$url = $uri->toString( array('scheme', 'host', 'port', 'path'));

	$target 		= $pluginParams->get('target','_blank');
	$size 		= $pluginParams->get('size','32');
	$googlerow		= $pluginParams->get('googlerow','newrow');
	$align 		= $pluginParams->get('align','left'); 
	$margin		= intval($pluginParams->get('margin','3px')); 
	$text 		= $pluginParams->get('text','Bookmark this page on'); 
	$credits     	= $pluginParams->get('credits','1'); 
	$size2 	     	= intval($size) + intval($margin);
	$effect		= $pluginParams->get('effect','none');
	$ssize		= intval($size) - 4;
	$ssizeoffset	= intval($size) - 2 + intval($margin);
	$gsize		= intval($size) + 4;
	$gsizeoffset	= intval($size) + 2 + intval($margin);
	$modcolor		= $pluginParams->get('modcolor','#ffffff');
	$shiftoffset	= intval($size) + intval($margin);
	$highlight		= $pluginParams->get('highlight','#fff000');
	$rotatedeg		= $pluginParams->get('rotatedeg',"90");
	$padbottom		= intval($pluginParams->get('padbottom', '10'));
	$orientation	= $pluginParams->get('orientation', 'right');
	$toporbot		= $pluginParams->get('toporbot', 'bottom');
	$padrorl		= intval($pluginParams->get('padrorl', '0'));
	$sidebartitle	= $pluginParams->get('sidebartitle', 'yes');

	if ($orientation == 'right'){
		$shiftside = 'left';
			}else{
		$shiftside = 'right';
		}



// Get Icon Parameters
$ic = array(
	$this->params->get('ic1'), $this->params->get('ic2'), $this->params->get('ic3'));

$icurl = array(
	$this->params->get('url1'), $this->params->get('url2'), $this->params->get('url3'));

	$vimg = array();
	$vurl = array();


	$ii=-1;
	$iii=0;	
	for($i=0;$i < count($ic);$i++)
		{
			$vimg[$ic[$i]]= htmlspecialchars($icurl[$i]);
		 	if(($vimg[$ic[$i]]) != '') {
				$ii++;
			}
		}

	if((intval($ii) > -1)&&($sidebartitle == 'yes')){
			if($orientation == 'left'){
				$follow = 'followus.png';
				$offset2 = intval($padrorl) + intval($size) + 5;
			}else{
				$follow = 'followus2.png';
				$offset2 = intval($padrorl) + intval($size) + 5;
			}

			$offset = ((((intval($ii) * (intval($size) + intval($margin))))) / 2) - 48 + intval($padbottom) + (intval($size) / 2);
			echo '<div style="position:fixed;'. $orientation .':'. $offset2 .'px;'. $toporbot .':'. $offset. 'px;width:32px;z-index:1000;"><img src="plugins/system/SocialMediaLinks/linksicons/'. $follow .'" width="16" height="96" /></div>';
			}

		

	for($i=0;$i < count($ic);$i++)
		{
			$vimg[$ic[$i]]= htmlspecialchars($icurl[$i]);
			$vurl[$icurl[$i]]=$ic[$i];
			$title2 = ucwords(substr($vurl[$icurl[$i]], 0 , -4));

		
			$offset = intval($padbottom) + (((intval($ii) * (intval($size) + intval($margin)))) - ((intval($iii) * (intval($size) + intval($margin)))));
		

	 	 	if(($vimg[$ic[$i]]) != '') {
					$iii++;
				echo '<div style="position:fixed;'. $orientation .':'. $padrorl .'px;'. $toporbot .':'. $offset. 'px;width:'. $size .'px;z-index:1000;"><a target="_blank" rel="nofollow" href="'. $vimg[$ic[$i]]. '"><img border="0" src="plugins/system/SocialMediaLinks/linksicons/'. $vurl[$icurl[$i]] .' " width="'. $size .'" height="'. $size .'" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				echo 'title="'. $rsstext .' '. $title2 .'" /></a></div>';
			}else{
				echo 'title="'. $text .' '. $title2 .'" /></a></div>';
			}

			switch ($effect) {
    				case "none":
        			break;
    				case "custom":
					echo '<div style="position:fixed;'. $orientation .':'. $padrorl .'px;'. $toporbot .':'. $offset. 'px;width:'. $size .'px;z-index:1000;"><a target="_blank" rel="nofollow" href="'. $vimg[$ic[$i]]. '"><img style="opacity:0.0;filter:alpha(opacity=0);" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.0;this.filters.alpha.opacity=0" border="0" src="plugins/system/SocialMediaLinks/linksicons/mo/'. $vurl[$icurl[$i]] .' " width="'. $size .'" height="'. $size .'" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				echo 'title="'. $rsstext .' '. $title2 .'" /></a></div>';
			}else{
				echo 'title="'. $text .' '. $title2 .'" /></a></div>';
			}
        			break;
    				case "dogear":
					echo '<div style="position:fixed;'. $orientation .':'. $padrorl .'px;'. $toporbot .':'. $offset. 'px;width:'. $size .'px;z-index:1000;"><a target="_blank" rel="nofollow" href="'. $vimg[$ic[$i]]. '"><img style="opacity:0.0;filter:alpha(opacity=0);" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.0;this.filters.alpha.opacity=0" border="0" src="plugins/system/SocialMediaLinks/linksicons/mo/dogear.png" width="'. $size .'" height="'. $size .'" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				echo 'title="'. $rsstext .' '. $title2 .'" /></a></div>';
			}else{
				echo 'title="'. $text .' '. $title2 .'" /></a></div>';
			}
        			break;
				case "enlarge":
					echo '<div style="position:fixed;'. $orientation .':'. $padrorl .'px;'. $toporbot .':'. $offset. 'px;width:'. $gsize .'px;z-index:1000;"><a target="_blank" rel="nofollow" href="'. $vimg[$ic[$i]]. '"><img style="margin-bottom: -2px; opacity:0.0;filter:alpha(opacity=0);" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.0;this.filters.alpha.opacity=0" border="0" src="plugins/system/SocialMediaLinks/linksicons/'. $vurl[$icurl[$i]] .' " width="'. $gsize .'" height="'. $gsize .'" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				echo 'title="'. $rsstext .' '. $title2 .'" /></a></div>';
			}else{
				echo 'title="'. $text .' '. $title2 .'" /></a></div>';
			}
        			break;
				case "highlight":
					echo '<div style="position:fixed;'. $orientation .':'. $padrorl .'px;'. $toporbot .':'. $offset. 'px;width:'. $ssize .'px;z-index:1000;"><a target="_blank" rel="nofollow" href="'. $vimg[$ic[$i]]. '"><img style="opacity:0.0;filter:alpha(opacity=0); margin-'. $shiftside .': -4px; border:2px solid '. $highlight .';" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.0;this.filters.alpha.opacity=0" border="0" src="plugins/system/SocialMediaLinks/linksicons/'. $vurl[$icurl[$i]] .' " width="'. $size .'" height="'. $size .'" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				echo 'title="'. $rsstext .' '. $title2 .'" /></a></div>';
			}else{
				echo 'title="'. $text .' '. $title2 .'" /></a></div>';
			}
        			break;
				case "rotate":
					echo '<div style="position:fixed;'. $orientation .':'. $padrorl .'px;'. $toporbot .':'. $offset. 'px;width:'. $size .'px;z-index:1000;"><a target="_blank" rel="nofollow" href="'. $vimg[$ic[$i]]. '"><img style="-moz-transform: rotate('.$rotatedeg.'deg); -ms-transform: rotate('.$rotatedeg.'deg); opacity:0.0;filter:alpha(opacity=0);" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.0;this.filters.alpha.opacity=0" border="0" src="plugins/system/SocialMediaLinks/linksicons/'. $vurl[$icurl[$i]] .' " width="'. $size .'" height="'. $size .'" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				echo 'title="'. $rsstext .' '. $title2 .'" /></a></div>';
			}else{
				echo 'title="'. $text .' '. $title2 .'" /></a></div>';
			}
        			break;
				case "bullseye":
					echo '<div style="position:fixed;'. $orientation .':'. $padrorl .'px;'. $toporbot .':'. $offset. 'px;width:'. $size .'px;z-index:1000;"><a target="_blank" rel="nofollow" href="'. $vimg[$ic[$i]]. '"><img style="opacity:0.0;filter:alpha(opacity=0);" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.0;this.filters.alpha.opacity=0" border="0" src="plugins/system/SocialMediaLinks/linksicons/mo/bullseye.png" width="'. $size .'" height="'. $size .'" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				echo 'title="'. $rsstext .' '. $title2 .'" /></a></div>';
			}else{
				echo 'title="'. $text .' '. $title2 .'" /></a></div>';
			}
        			break;
			}
		}
	}

		$str_html = ob_get_contents();
		ob_end_clean();
		
		$body = JResponse::getBody();
		$body = str_replace('</body>', $str_html.'</body>', $body);
		JResponse::setBody($body);
		
		return true;
	}

  function onContentBeforeDisplay($context, &$article, &$params, $page=0){
    $document = & JFactory::getDocument();
    $view     = JRequest::getCmd('view');
    $position = $this->params->get( 'position','1');
	$embed 	= $this->params->get('embed', 'no');

    if ((($view == 'article')&&($this->params->get('viewonarticle')=='yes'))|| 
        (($view == 'featured')&&($this->params->get('viewonfrontpage')=='yes'))||
        (($view == 'category')&&($this->params->get('viewoncategory')=='yes'))
       ) { 
    	$title= htmlentities( $article->title, ENT_QUOTES, "UTF-8");
      $url = $this->getNewUrl($article);
      $htmlCode = $this->getArticleHTML($params, $article, $url, $title);

      if ($view == 'article') {
        $tmp = $article->text;
      } else {
        $tmp = $article->introtext;
      }
	if (($embed == 'yes') && (strstr($tmp, '{socialmedialinks}'))) {
			$article2 = $this->rstrstr($tmp, '{socialmedialinks}');
			$article2 .= $htmlCode;
			$article2 .= substr(strstr($tmp, '{socialmedialinks}'), 18);
			$tmp = $article2;
				}else{
			 if ($position == '1'){
			        $tmp =  $htmlCode . $tmp;  
					      }
			 if ($position == '2'){
			        $tmp = $tmp . $htmlCode;
					      }
			 if ($position == '3'){
			        $tmp =  $htmlCode . $tmp . $htmlCode;
					      }
				}
      if ($view == 'article') {
        $article->text = $tmp;
      } else {
        $article->introtext = $tmp;
      }
    }else{
      return; 
    }
  }
  
  private function rstrstr($haystack,$needle)
    {
        return substr($haystack, 0,strpos($haystack, $needle));
    }

  private function  getArticleHTML($params, $article, $url, $title) { 
    	$ex_categories	= $this->params->get('ex_categories', '' );
    	$ex_articles	= $this->params->get('ex_articles', '' );
    	$ex_CatList	= @explode ( ",", $ex_categories ); 
    	$ex_ArticleList	= @explode ( ",", $ex_articles );  
    if ( in_array ( $article->id, $ex_ArticleList ) || in_array ( $article->catid, $ex_CatList ) ) return;
	$robots		= $this->params->get('robots','1');
	$text 		= $this->params->get('text','Follow us on'); 
	$rsstext 		= $this->params->get('rsstext','Subscribe to our Feed'); 

    	$size			= $this->params->get('size');
    	$css			= $this->params->get('css');
	$margin		= $this->params->get('margin', '2');
	$margin		= intval($margin);
	$size2 	     	= intval($size) + intval($margin);
	$size3		= intval($size2) + 2;
	$effect		= $this->params->get('effect','none');
	$ssize		= intval($size) - 4;
	$ssizeoffset	= intval($size) - 2 + intval($margin);
	$gsize		= intval($size) + 4;
	$gsizeoffset	= intval($size) + 2 + intval($margin);
	$modcolor		= $this->params->get('modcolor','#ffffff');
	$shiftoffset	= intval($size) + intval($margin);
	$highlight		= $this->params->get('highlight','#fff000');
	$rotatedeg		= $this->params->get('rotatedeg',"90");
    	$htmlCode		= "";

// Prepare the Link Attribute
	if($robots == '1') {
	$nofollow = 'rel="nofollow"';
	}else{
	$nofollow = '';
	}

// Get Icon Parameters
$ic = array(
	$this->params->get('ic1'), $this->params->get('ic2'), $this->params->get('ic3'));

$icurl = array(
	$this->params->get('url1'), $this->params->get('url2'), $this->params->get('url3'));


	$vimg = array();
	$vurl = array();

	$htmlCode = "";

	$htmlCode .= '<div style="' .$css. '">';

// Prepare the Icon List
	for($i=0;$i < count($ic);$i++)
     {
	$vimg[$ic[$i]]= htmlspecialchars($icurl[$i]);
	$vurl[$icurl[$i]]=$ic[$i];
	$title2 = ucwords(substr($vurl[$icurl[$i]], 0 , -4));


// Output the Icon Links	
	 	 if(($vimg[$ic[$i]]) != '') {
		 	$htmlCode .= '<a '. $nofollow .' href="'. $vimg[$ic[$i]]. '" target="'. $target .'">';
			$htmlCode .= '<img style="width: '.$size.'px; height: '.$size.'px; margin:'. $margin .'px;" src="plugins/system/SocialMediaLinks/linksicons/'. $vurl[$icurl[$i]] .' " alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				$htmlCode .= 'title="'. $rsstext .' '. $title2 .'" /></a>';
			}else{
				$htmlCode .= 'title="'. $text .' '. $title2 .'" /></a>';
			}

		switch ($effect) {
			case "none":
				break;
			case "custom":
					$htmlCode .= '<a href='.$vimg[$ic[$i]].' target="_blank" rel="nofollow">';
					$htmlCode .= '<img style="width: '.$size.'px; height: '.$size.'px; opacity:0.0;filter:alpha(opacity=0); position: relative; margin-bottom: '. $margin .'px; margin-left: -'. $size2 .'px; z-index: 2;" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.0;this.filters.alpha.opacity=0" src="plugins/system/SocialMediaLinks/linksicons/mo/'.$vurl[$icurl[$i]].'" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				$htmlCode .= 'title="'. $rsstext .' '. $title2 .'" /></a>';
			}else{
				$htmlCode .= 'title="'. $text .' '. $title2 .'" /></a>';
			}
				break;
			case "dogear":
					$htmlCode .= '<a href='.$vimg[$ic[$i]].' target="_blank" rel="nofollow">';
					$htmlCode .= '<img style="width: '.$size.'px; height: '.$size.'px; opacity:0.0;filter:alpha(opacity=0); position: relative; margin-bottom: '. $margin .'px; margin-left: -'. $size2 .'px; z-index: 2;" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity= 0.0;this.filters.alpha.opacity=0" src="plugins/system/SocialMediaLinks/linksicons/mo/dogear.png" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				$htmlCode .= 'title="'. $rsstext .' '. $title2 .'" /></a>';
			}else{
				$htmlCode .= 'title="'. $text .' '. $title2 .'" /></a>';
			}
				break;
			case "shrink":
					$htmlCode .= '<a href='.$vimg[$ic[$i]].' target="_blank" rel="nofollow">';
					$htmlCode .= '<img style="width: '.$ssize.'px; height: '.$ssize.'px; opacity:0.0;filter:alpha(opacity=0); position: relative; margin-bottom: '. $margin .'px; border: 3px solid '. $modcolor .'; margin-left: -'. $size2 .'px; z-index: 4;" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"
onmouseout="this.style.opacity=0.0;this.filters.alpha.opacity=0" src="plugins/system/SocialMediaLinks/linksicons/'.$vurl[$icurl[$i]].'" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				$htmlCode .= 'title="'. $rsstext .' '. $title2 .'" /></a>';
			}else{
				$htmlCode .= 'title="'. $text .' '. $title2 .'" /></a>';
			}
				break;
			case "enlarge":
					$htmlCode .= '<a href='.$vimg[$ic[$i]].' target="_blank" rel="nofollow">';
					$htmlCode .= '<img style="width: '.$gsize.'px; height: '.$gsize.'px; opacity:0.0;filter:alpha(opacity=0); position: relative; margin-bottom: '. $margin .'px; margin-left: -'. $gsizeoffset .'px; z-index: 2;" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.0;this.filters.alpha.opacity=0" src="plugins/system/SocialMediaLinks/linksicons/'.$vurl[$icurl[$i]].'" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				$htmlCode .= 'title="'. $rsstext .' '. $title2 .'" /></a>';
			}else{
				$htmlCode .= 'title="'. $text .' '. $title2 .'" /></a>';
			}
				break;
			case "shiftup":
					$htmlCode .= '<a href='.$vimg[$ic[$i]].' target="_blank" rel="nofollow">';
					$htmlCode .= '<img style="width: '.$size.'px; height: '.$size.'px; opacity:0.0;filter:alpha(opacity=0); position: relative; margin-bottom: '. $margin .'px; border-bottom-style: solid; border-bottom-width: 3px; border-bottom-color: '. $modcolor .'; margin-left: -'. $shiftoffset .'px; z-index: 2;" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"
onmouseout="this.style.opacity=0.0;this.filters.alpha.opacity=0" src="plugins/system/SocialMediaLinks/linksicons/'.$vurl[$icurl[$i]].'" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				$htmlCode .= 'title="'. $rsstext .' '. $title2 .'" /></a>';
			}else{
				$htmlCode .= 'title="'. $text .' '. $title2 .'" /></a>';
			}
				break;
			case "highlight":
					$htmlCode .= '<a href='.$vimg[$ic[$i]].' target="_blank" rel="nofollow">';
					$htmlCode .= '<img style="width: '.$ssize.'px; height: '.$ssize.'px; opacity:0.0;filter:alpha(opacity=0); position: relative; margin-bottom: '. $margin .'px; border:2px solid '. $highlight .'; margin-left: -'. $size2 .'px; z-index: 4;" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"
onmouseout="this.style.opacity=0.0;this.filters.alpha.opacity=0" src="plugins/system/SocialMediaLinks/linksicons/'.$vurl[$icurl[$i]].'" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				$htmlCode .= 'title="'. $rsstext .' '. $title2 .'" /></a>';
			}else{
				$htmlCode .= 'title="'. $text .' '. $title2 .'" /></a>';
			}
				break;
			case "rotate":
					$htmlCode .= '<a href='.$vimg[$ic[$i]].' target="_blank" rel="nofollow">';
					$htmlCode .= '<img style="width: '.$size.'px; height: '.$size.'px; -moz-transform: rotate('.$rotatedeg.'deg); -ms-transform: rotate('.$rotatedeg.'deg); opacity:0.0;filter:alpha(opacity=0); position: relative; margin-bottom: '. $margin .'px; margin-left: -'. $size2 .'px; z-index: 2;" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"
onmouseout="this.style.opacity=0.0;this.filters.alpha.opacity=0" src="plugins/system/SocialMediaLinks/linksicons/'.$vurl[$icurl[$i]].'" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				$htmlCode .= 'title="'. $rsstext .' '. $title2 .'" /></a>';
			}else{
				$htmlCode .= 'title="'. $text .' '. $title2 .'" /></a>';
			}
				break;
			case "bullseye":
					$htmlCode .= '<a href='.$vimg[$ic[$i]].' target="_blank" rel="nofollow">';
					$htmlCode .= '<img style="width: '.$size.'px; height: '.$size.'px; opacity:0.0;filter:alpha(opacity=0); position: relative; margin-bottom: '. $margin .'px; margin-left: -'. $size2 .'px; z-index: 2;" onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100" onmouseout="this.style.opacity=0.0;this.filters.alpha.opacity=0" src="plugins/system/SocialMediaLinks/linksicons/mo/bullseye.png" alt="'. $title2 .'" ';
			if($title2 == 'Feed') {
				$htmlCode .= 'title="'. $rsstext .' '. $title2 .'" /></a>';
			}else{
				$htmlCode .= 'title="'. $text .' '. $title2 .'" /></a>';
			}
				break;
					}
			}
	}

			$htmlCode .= '</div>';

    return $htmlCode; 
  }
  
  private function getNewUrl($obj){
    if (!is_null($obj)) {
      $url = JRoute::_(ContentHelperRoute::getArticleRoute($obj->slug, $obj->catslug, $obj->sectionid));
      $uri = JURI::getInstance();
      $base  = $uri->toString( array('scheme', 'host', 'port'));
      $url = $base . $url;
      $url = JRoute::_($url, true, 0);
      return $url;
    }
  }
}
?>

